package com.hiep.connect.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.hiep.connect.shared.MyUser;

public class UiUtils {

	public static SimplePanel getLogoPanel() {
		SimplePanel logoPanel = new SimplePanel();

		HTML logo = new HTML(
				"<a href='http://cooltext.com'><img src='http://images.cooltext.com/2604741.png' width='150' height='50' alt='connect' /></a>");
		// HTML logo = new HTML(
		// "<a href='http://cooltext.com'><img src='http://images.cooltext.com/2611302.png' width='400' height='38' alt='CS251B Connect' /></a>");

		logoPanel.add(logo);

		return logoPanel;
	}

	public static HorizontalPanel getSouthPanel() {
		Hyperlink policyLink = new Hyperlink("Policy", "policy");
		Hyperlink helpLink = new Hyperlink("Help", "help");
		Hyperlink aboutLink = new Hyperlink("About", "about");
		Label copyRight = new HTML("&copy; Copyright 2012-CS251B_SJSU");

		FlexTable flexTable = new FlexTable();
		flexTable.setWidth("400px");
		// flexTable.setCellSpacing(10);
		flexTable.setWidget(0, 1, policyLink);
		flexTable.setWidget(0, 3, helpLink);
		flexTable.setWidget(0, 5, aboutLink);
		flexTable.setWidget(0, 7, copyRight);
		HorizontalPanel southPanel = new HorizontalPanel();
		southPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		southPanel.add(flexTable);
		southPanel.setStylePrimaryName("hiep-South");
		southPanel.setWidth("100%");
		return southPanel;
	}

	public static StackPanel getNavigationPanel() {
		StackPanel navigationPanel = new StackPanel();
		String text1 = "Lorem ipsum dolor sit amet...";
		String text2 = "Sed egestas, arcu nec accumsan...";
		String text3 = "Proin tristique, elit at blandit...";
		Label label;
		label = new Label(text1);
		navigationPanel.add(getFriendTree(), "Friend", false);

		label = new Label(text2);
		navigationPanel.add(label, "Event", false);
		label = new Label(text3);

		Hyperlink loginInfoLink = new Hyperlink("User Info", "list");
		Hyperlink privacyLink = new Hyperlink("Privacy Setting", "login");
		VerticalPanel linkPanel = new VerticalPanel();
		linkPanel.add(loginInfoLink);
		linkPanel.add(privacyLink);

		navigationPanel.add(linkPanel, "Account Setting", false);
		navigationPanel.setSize("200px", "550px");
		navigationPanel.addStyleName("demo-panel");

		return navigationPanel;
	}

	public static StackPanel getNavigationPanel(MyUser user) {
		StackPanel navigationPanel = new StackPanel();
		String text1 = "Lorem ipsum dolor sit amet...";
		String text2 = "Sed egestas, arcu nec accumsan...";
		String text3 = "Proin tristique, elit at blandit...";
		Label label;
		label = new Label(text1);
		navigationPanel.add(getFriendTree(user), "Friend", false);

		label = new Label(text2);
		navigationPanel.add(label, "Event", false);
		label = new Label(text3);

		Hyperlink loginInfoLink = new Hyperlink("User Info", "list");
		Hyperlink privacyLink = new Hyperlink("Privacy Setting", "login");
		VerticalPanel linkPanel = new VerticalPanel();
		linkPanel.add(loginInfoLink);
		linkPanel.add(privacyLink);

		navigationPanel.add(linkPanel, "Account Setting", false);
		navigationPanel.setSize("200px", "550px");
		navigationPanel.addStyleName("demo-panel");

		return navigationPanel;
	}

	public static Tree getFriendTree() {
		Tree tree = new Tree();

		TreeItem outerRoot = new TreeItem("Item 1");
		outerRoot.addItem("Item 1-1");
		outerRoot.addItem("Item 1-2");
		outerRoot.addItem("Item 1-3");
		outerRoot.addItem(new CheckBox("Item 1-4"));
		tree.addItem(outerRoot);

		TreeItem innerRoot = new TreeItem("Item 1-5");
		innerRoot.addItem("Item 1-5-1");
		innerRoot.addItem("Item 1-5-2");
		innerRoot.addItem("Item 1-5-3");
		innerRoot.addItem("Item 1-5-4");
		innerRoot.addItem(new CheckBox("Item 1-5-5"));

		outerRoot.addItem(innerRoot);
		return tree;
	}

	public static Tree getFriendTree(MyUser user) {
		// TODO
		// List<FriendGroup> friendGroups = user.getFriendGroups();
		List<MyUser> friends = user.getFriends();
		Tree tree = new Tree();

		// if (friendGroups.size() > 0) {
		// for (FriendGroup group : friendGroups) {
		// TreeItem groupItem = new TreeItem(group.getName());
		//
		// List<MyUser> members = group.getMembers();
		// for (MyUser u : members) {
		// // Add friends to group.
		// groupItem.addItem(u.getEmail());
		// }
		// // Add group to tree.
		// tree.addItem(groupItem);
		// }
		// } else
		if (friends.size() > 0) {
			// TODO process friends
			for (MyUser friend : friends) {
				TreeItem friendItem = new TreeItem(friend.getEmail());
				tree.addItem(friendItem);
			}
		}

		return tree;
	}

	public static MenuBar getMenuBar() {
		// Create a command that will execute on menu item selection
		Command menuCommand = new Command() {
			private int curPhrase = 0;
			private final String[] phrases = { "phase 1", "phase 2" };

			public void execute() {
				Window.alert(phrases[curPhrase]);
				curPhrase = (curPhrase + 1) % phrases.length;
			}
		};

		// Create a menu bar
		MenuBar menu = new MenuBar();
		// menu.setStylePrimaryName("td.contacts-ListMenu");
		menu.setAutoOpen(true);
		// menu.setWidth("500px");
		menu.setAnimationEnabled(true);

		// Create the file menu
		// MenuBar fileMenu = new MenuBar(true);
		// fileMenu.setAnimationEnabled(true);
		// menu.addItem(new MenuItem("Account Setting", fileMenu));
		// String[] fileOptions = { "Options 1", "Options 2" };
		// for (int i = 0; i < fileOptions.length; i++) {
		// if (i == 3) {
		// fileMenu.addSeparator();
		// // fileMenu.addItem(fileOptions[i], recentDocsMenu);
		// fileMenu.addSeparator();
		// } else {
		// fileMenu.addItem(fileOptions[i], menuCommand);
		// }
		// }

		// Create the help menu
		MenuBar gwtMenu = new MenuBar(true);
		menu.addItem(new MenuItem("Help", true, gwtMenu));
		String[] gwtOptions = { "Help Options 1", "Help Options 2" };
		for (int i = 0; i < gwtOptions.length; i++) {
			gwtMenu.addItem(gwtOptions[i], menuCommand);
		}

		// Create the logout menu
		menu.addSeparator();
		Command logoutCommand = new Command() {

			@Override
			public void execute() {
				// TODO Publish logout event
				Window.alert("Logout");

			}
		};
		MenuItem logoutItem = new MenuItem("Logout", logoutCommand);
		logoutItem.setTitle("Log out of this account");
		menu.addItem(logoutItem);

		// Return the menu
		menu.ensureDebugId("cwMenuBar");
		return menu;
	}

	public static FormPanel createFileUpload() {
		final FormPanel form = new FormPanel();
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.addStyleName("table-center");
		form.addStyleName("demo-panel-padded");
		form.setWidth("275px");

		HorizontalPanel holder = new HorizontalPanel();

		final FileUpload upload = new FileUpload();
		upload.setName("upload");
		holder.add(upload);

		// holder.add(new HTML("<hr />"));

		holder.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
		Button submitButton = new Button("Upload");
		submitButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				String filename = upload.getFilename();
				Window.alert(filename + " uploaded");

			}
		});

		holder.add(submitButton);
		form.add(holder);

		// form.setAction("url");

		return form;
	}

}
